var eventsMgr = require('eventsMgr');
var consts = require('consts');
var yySpine = require('yySpine');
var actions = require('actions');
let soundMgr = require('soundMgr');

cc.Class({
    extends: cc.Component,

    properties: {

        feiJi: cc.Node,
        speed: 10,
        movetype: 0,
        xue: 3,
    },

    onLoad() {
        this.zhong = 0;
        this.isCol = true;
        this.width = cc.winSize.width;

    },

    onCollisionEnter: function (other, self) {
        this.xue = 1;
        if(!this.isCol) return;
        if (other.node.name == "zidan") {
            // soundMgr.playSoundEffectByName("aida");
            soundMgr.playSoundEffectByName("aida");
            other.node.destroy();
            this.zhong++;
            console.log('碰撞了么？sssssssss', this.zhong);
            // yySpine.playAnimation(self.node.children[0], 'damage', 1, true, () => {
            //     yySpine.playAnimation(self.node.children[0], 'walk', 0, true);
            // } )
            if(this.zhong == this.xue){
                this.isCol = false;
                yySpine.playAnimation(this.feiJi, 'idle', 0, true);
                console.log('ddddddddddddd');
                if(this.movetype == 6 || this.movetype == 4){
                    yySpine.playAnimation(self.node.children[0], 'dead', 1, true, () => {
                        self.node.children[0].scaleX = -1;
                        yySpine.playAnimation(self.node.children[0], 'run', 0, true);
                        actions.moveBy(self.node.children[0], 2, this.width, 0, () => {
                            self.node.destroy();
                        })
                        
                    }); 
                }else{
                    yySpine.playAnimation(self.node.children[0], 'dead', 1, true, () => {
                        self.node.destroy();
                    });
                    // setTimeout(() => {

                    // }, 1000);
                }

                // yySpine.playAnimation(self.node.children[0], 'dead', 1, true, () => {
                //     self.node.destroy();
                // });
                eventsMgr.emit('GUAI_DEATH');
            }
        }
    },


    update(dt) {
        let self = this;
        if (window.canMove == this.movetype) {
            if (self.node.x - 150 > self.feiJi.x) {
                self.node.x -= dt * this.speed;
            }
            if (self.node.y > self.feiJi.y - 50) {
                self.node.y -= dt * this.speed;
            } else if (self.node.y < self.feiJi.y) {
                self.node.y += dt * this.speed;
            }
        }

    },


});
